# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

# The patches for this package are developed inside the -git package.
# This is for three reasons:
# 1. Upstreaming is easier.
# 2. The Qt devs often forget to release Qbs at the same time as Qt Creator.
# 3. Commit messages are retained (though efforts are made for that via git init)

# TODO :: Patch this for clang support:
#         qbs setup-toolchains --type clang /mingw64/bin/clang.exe clang
#         .. will add a profile to %AppData%/QtProject/qbs.ini that contains:
#         qt-project\qbs\profiles\clang\qbs\toolchain=clang, llvm, gcc
#         .. but is missing:
#         qt-project\qbs\profiles\clang\qbs\targetOS=windows
#         then in share/qbs/modules/cpp/windows-mingw.qbs we need to change
#         condition: qbs.targetOS.contains("windows") && qbs.toolchain.contains("mingw")
#         .. to:
#         condition: qbs.targetOS.contains("windows") \
#               && (qbs.toolchain.contains("mingw") || qbs.toolchain.contains("gcc"))

_realname=qbs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.1
pkgrel=1
pkgdesc="Qt Build Suite (mingw-w64)"
url='https://www.qt.io/'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-python-beautifulsoup4"
             "${MINGW_PACKAGE_PREFIX}-python-lxml"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")  # clang for qdoc
_pkgfqn="${_realname}-src-${pkgver}"
source=("https://download.qt.io/official_releases/qbs/${pkgver}/${_pkgfqn}.tar.gz"
        "manifest"
        "0002-remove-lib-prefix-from-plugin-names.patch"
        "0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch")
sha256sums=('3acd5704494777f185ba64a47adfb8a690086f007ad0e3d296f32bcb21f72843'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855'
            'e62bfe3d1958ec0a83010d9fb27def32024839de9dbcd7088c21f08e1460c7e1'
            '8d511ec7252b658e0d020fb2f662675d5e5506f213d6c75bfbe3619b540d4d52')

prepare() {
  cd ${_pkgfqn}

  patch -p1 -i "${srcdir}"/0002-remove-lib-prefix-from-plugin-names.patch
  patch -p1 -i "${srcdir}"/0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DWITH_TESTS=OFF \
    -DQBS_LIB_INSTALL_DIR=lib \
    -DQBS_DLL_INSTALL_DIR=bin \
    -DQBS_INSTALL_HTML_DOCS=ON \
    -DQBS_INSTALL_QCH_DOCS=ON \
    -DQBS_INSTALL_MAN_PAGE=ON \
    ../${_pkgfqn}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  for ff in qbs-setup-toolchains qbs-setup-qt; do
    cp ${srcdir}/manifest ${pkgdir}${MINGW_PREFIX}/bin/${ff}.exe.manifest
  done
  cp ${srcdir}/${_pkgfqn}/VERSION ${pkgdir}${MINGW_PREFIX}/include/qbs
}
